﻿using System;
using System.Collections.Generic;
using System.Text;

namespace QuickWeather.Core
{
    public class WeatherProvider
    {
        public delegate void UpdateHandler(object sender, WeatherUpdatedEventArgs e);
        public event UpdateHandler Updated;

        private TrayWeather.Utils.DownloadText downloader = new TrayWeather.Utils.DownloadText();
        private System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();

        /// <summary>
        /// Provide simple access to weather info
        /// </summary>
        public WeatherProvider()
        {
            //Загружаем XML настройки провайдера

            downloader.DataDownloaded += new TrayWeather.Utils.DownloadText.DownloadedEventHandler(downloader_DataDownloaded);
            timer.Tick += new EventHandler(timer_Tick);
        }

        /// <summary>
        /// Provide simple access to weather info
        /// </summary>
        /// <param name="autoUpdate">Enable autoupdate</param>
        /// <param name="updateInterval">Autoupdate interval</param>
        public WeatherProvider(bool autoUpdate, int updateInterval)
            : this()
        {
            this.AutoUpdate = autoUpdate;
            this.Interval = updateInterval;
        }

        /// <summary>
        /// Request new weather
        /// </summary>
        public void Update()
        {
            downloader.GetTextAsync(new Uri("http://example.com"), Encoding.UTF8);
        }

        /// <summary>
        /// Get weather state
        /// </summary>
        /// <returns>Return <seealso cref="TrayWeather.Weather"/> with current weather state</returns>
        public Weather GetWeather()
        {
            this.weather = Parse(downloader.GetText(new Uri("http://example.com"), Encoding.UTF8), true);
            return this.weather; 
        }

        /// <summary>
        /// Parse incoming data to TrayWeather.Weather
        /// </summary>
        /// <param name="Data">Raw weather data</param>
        /// <returns>Return <seealso cref="TrayWeather.Weather"/> with current weather state</returns>
        private Weather Parse(string Data, bool flag)
        {
            //Загружаем XSLT и преобразовываем полученный поток данных. Результат рапарсиваем и ретарним
            return this.weather;
        }

        /// <summary>
        /// Parse incoming data to TrayWeather.Weather
        /// </summary>
        /// <param name="Data">Raw weather data</param>
        /// <returns>Return <seealso cref="TrayWeather.Weather"/> with current weather state</returns>
        private void Parse(string Data)
        {
            //Загружаем XSLT и преобразовываем полученный поток данных. Результат рапарсиваем в Weather и генерим событие обновления
            Updated(this, new WeatherUpdatedEventArgs());
        }

        private void timer_Tick(object sender, EventArgs e)
        {
            Update();
        }

        private void downloader_DataDownloaded(string Result)
        {
            if (!string.IsNullOrEmpty(Result))
            {
                Parse(Result);
            }
        }

        /// <summary>
        /// Gets or sets autoupdate feature enabled status
        /// </summary>
        private bool autoUpdate = false;
        public bool AutoUpdate
        {
            get { return autoUpdate; }

            set
            {
                if (value && timer.Enabled)
                    timer.Stop();
                else if (!value && !timer.Enabled)
                    timer.Start();

                autoUpdate = value;
            }
        }

        /// <summary>
        /// Gets or sets autoupdate interval in milliseconds
        /// </summary>
        public int Interval
        {
            get { return timer.Interval; }
            set { timer.Interval = value; }
        }

        /// <summary>
        /// Gets weather data
        /// </summary>
        private Weather weather = new Weather();
        public Weather Weather
        {
            get { return weather; }
        }
    }
}
